home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH2 / SRC / META.FRM < prev    next >
Text File  |  1996-04-18  |  7KB  |  256 lines

  1. VERSION 4.00
  2. Begin VB.Form MetaForm 
  3.    AutoRedraw      =   -1  'True
  4.    Caption         =   "Metafile"
  5.    ClientHeight    =   4680
  6.    ClientLeft      =   1950
  7.    ClientTop       =   1110
  8.    ClientWidth     =   5415
  9.    Height          =   5370
  10.    Left            =   1890
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   312
  13.    ScaleMode       =   3  'Pixel
  14.    ScaleWidth      =   361
  15.    Top             =   480
  16.    Width           =   5535
  17.    Begin MSComDlg.CommonDialog FileDialog 
  18.       Left            =   0
  19.       Top             =   0
  20.       _Version        =   65536
  21.       _ExtentX        =   847
  22.       _ExtentY        =   847
  23.       _StockProps     =   0
  24.       CancelError     =   -1  'True
  25.       Flags           =   2
  26.    End
  27.    Begin VB.Menu mnuFile 
  28.       Caption         =   "&File"
  29.       Begin VB.Menu mnuFileSaveAs 
  30.          Caption         =   "&Save As..."
  31.          Enabled         =   0   'False
  32.          Shortcut        =   ^S
  33.       End
  34.       Begin VB.Menu mnuFileLoad 
  35.          Caption         =   "&Load..."
  36.          Shortcut        =   ^L
  37.       End
  38.       Begin VB.Menu mnuFileClear 
  39.          Caption         =   "&Clear"
  40.       End
  41.       Begin VB.Menu mnuFileSep 
  42.          Caption         =   "-"
  43.       End
  44.       Begin VB.Menu mnuFileExit 
  45.          Caption         =   "E&xit"
  46.       End
  47.    End
  48. End
  49. Attribute VB_Name = "MetaForm"
  50. Attribute VB_Creatable = False
  51. Attribute VB_Exposed = False
  52. Option Explicit
  53.  
  54. Dim Drawing As Boolean
  55. Dim MetafileLoaded As Boolean
  56. Dim PointX() As Single
  57. Dim PointY() As Single
  58. Dim NumPoints As Integer
  59. Dim LastPoint As Integer
  60. Dim LastX As Single
  61. Dim LastY As Single
  62.  
  63.  
  64. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  65.     Drawing = True
  66.     LastX = x
  67.     LastY = y
  68.     AddPoint -x, y
  69. End Sub
  70. ' ***********************************************
  71. ' Add a point to the list of points.
  72. ' ***********************************************
  73. Sub AddPoint(x As Single, y As Single)
  74.     ' Start over if a metafile is displayed.
  75.     If MetafileLoaded Then
  76.         Cls
  77.         MetafileLoaded = False
  78.         LastPoint = 0
  79.     End If
  80.  
  81.     LastPoint = LastPoint + 1
  82.     If LastPoint > NumPoints Then
  83.         NumPoints = NumPoints + 100
  84.         ReDim Preserve PointX(1 To NumPoints)
  85.         ReDim Preserve PointY(1 To NumPoints)
  86.     End If
  87.     PointX(LastPoint) = x
  88.     PointY(LastPoint) = y
  89.  
  90.     If x < 0 Then
  91.         CurrentX = -x
  92.         CurrentY = y
  93.     Else
  94.         Line -(x, y)
  95.     End If
  96.     
  97.     mnuFileSaveAs.Enabled = True
  98. End Sub
  99.  
  100. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  101.     If Not Drawing Then Exit Sub
  102.     
  103.     AddPoint x, y
  104. End Sub
  105.  
  106. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
  107.     If Not Drawing Then Exit Sub
  108.     Drawing = False
  109.     
  110.     AddPoint x, y
  111. End Sub
  112.  
  113. Private Sub mnuFileClear_Click()
  114.     Cls
  115.     LastPoint = 0
  116.     mnuFileSaveAs.Enabled = False
  117. End Sub
  118.  
  119.  
  120. ' ***********************************************
  121. ' Load a metafile.
  122. ' ***********************************************
  123. Private Sub mnuFileLoad_Click()
  124. Dim fname As String
  125. Dim mhdc As Integer
  126. Dim hMF As Integer
  127. Dim status As Long
  128.  
  129.     ' Allow the user to pick a file.
  130.     On Error Resume Next
  131.     FileDialog.filename = "*.WMF"
  132.     FileDialog.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  133.     FileDialog.ShowOpen
  134.     If Err.Number = cdlCancel Then
  135.         Unload FileDialog
  136.         Exit Sub
  137.     ElseIf Err.Number <> 0 Then
  138.         Unload FileDialog
  139.         Beep
  140.         MsgBox "Error selecting file.", , vbExclamation
  141.         Exit Sub
  142.     End If
  143.     On Error GoTo 0
  144.     
  145.     fname = FileDialog.filename
  146.     FileDialog.InitDir = Left$(fname, Len(fname) _
  147.         - Len(FileDialog.FileTitle) - 1)
  148.  
  149.     MetafileLoaded = True
  150.     Cls
  151.     mnuFileSaveAs.Enabled = False
  152.     
  153.     ' Load the metafile.
  154.     hMF = GetMetaFile(fname)
  155.     If hMF = 0 Then
  156.         Beep
  157.         MsgBox "Unable to load metafile " & _
  158.             fname & ".", vbExclamation
  159.         Exit Sub
  160.     End If
  161.     
  162.     ' Play the metafile.
  163.     If PlayMetaFile(hdc, hMF) = 0 Then
  164.         Beep
  165.         MsgBox "Error playing metafile " & _
  166.             fname & ".", vbExclamation
  167.     End If
  168.     
  169.     ' Delete the metafile to free resources.
  170.     If DeleteMetaFile(hMF) = 0 Then
  171.         Beep
  172.         MsgBox "Error deleting metafile " & _
  173.             fname & ".", vbExclamation
  174.     End If
  175.     Refresh
  176. End Sub
  177.  
  178. Private Sub mnuFileExit_Click()
  179.     Unload Me
  180. End Sub
  181.  
  182. ' ***********************************************
  183. ' Save the drawing in a metafile.
  184. ' ***********************************************
  185. Private Sub mnuFileSaveAs_Click()
  186. Dim fname As String
  187. Dim i As Integer
  188. Dim mhdc As Integer
  189. Dim hMF As Integer
  190. Dim status As Long
  191. Dim x As Single
  192. Dim y As Single
  193.  
  194.     ' Allow the user to pick a file.
  195.     On Error Resume Next
  196.     FileDialog.filename = "*.WMF"
  197.     FileDialog.Flags = cdlOFNOverwritePrompt + _
  198.         cdlOFNPathMustExist + cdlOFNHideReadOnly
  199.     FileDialog.ShowSave
  200.     If Err.Number = cdlCancel Then
  201.         Unload FileDialog
  202.         Exit Sub
  203.     ElseIf Err.Number <> 0 Then
  204.         Unload FileDialog
  205.         Beep
  206.         MsgBox "Error selecting file.", , vbExclamation
  207.         Exit Sub
  208.     End If
  209.     On Error GoTo 0
  210.     
  211.     fname = FileDialog.filename
  212.     FileDialog.InitDir = Left$(fname, Len(fname) _
  213.         - Len(FileDialog.FileTitle) - 1)
  214.  
  215.     ' Create the metafile.
  216.     mhdc = CreateMetaFile(ByVal fname)
  217.     If mhdc = 0 Then
  218.         Beep
  219.         MsgBox "Error creating metafile " & _
  220.             fname & ".", vbExclamation
  221.         Exit Sub
  222.     End If
  223.     
  224.     ' Draw in the metafile.
  225.     For i = 1 To LastPoint
  226.         x = PointX(i)
  227.         y = PointY(i)
  228.         If x < 0 Then
  229. #If Win32 Then
  230.             status = MoveToEx(mhdc, -x, y, ByVal 0&)
  231. #Else
  232.             status = MoveTo(mhdc, -x, y)
  233. #End If
  234.         Else
  235.             status = LineTo(mhdc, x, y)
  236.         End If
  237.     Next i
  238.     
  239.     ' Close the metafile.
  240.     hMF = CloseMetaFile(mhdc)
  241.     If hMF = 0 Then
  242.         Beep
  243.         MsgBox "Error closing metafile " & _
  244.             fname & ".", vbExclamation
  245.     End If
  246.     
  247.     ' Delete the metafile to free resources.
  248.     If DeleteMetaFile(hMF) = 0 Then
  249.         Beep
  250.         MsgBox "Error deleting metafile " & _
  251.             fname & ".", vbExclamation
  252.     End If
  253. End Sub
  254.  
  255.  
  256.